package com.timemaneger.thinkintime;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.timemaneger.thinkintime.GridView.BookShelf;

import java.io.File;

public class ChooseBookActivity extends AppCompatActivity {

    private String path;
    private File[] subFile;
    private static int AddBook=100;
    private BookShelf bookShelf;
    private int[] bookdate;
    private String []name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_book);
        bookShelf=(BookShelf) findViewById(R.id.book_shelf);
        init();
    }

    private void init() {
        initBookDate();
        ShelfAdapter adapter=new ShelfAdapter();
        bookShelf.setAdapter(adapter);
        bookShelf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                if(position>=bookdate.length){

                }else{
                    if(position==0){
                        //添加书册
                        Intent intent=new Intent(ChooseBookActivity.this,AddBookActivity.class);
                        startActivityForResult(intent,AddBook);
                    }else {
                        Intent intent = new Intent();
                        intent.putExtra("name", subFile[position-1].getName());
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }
            }
        });
    }


    private void initBookDate() {
        path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/Html";
        File dir=new File(path);
        subFile=dir.listFiles();
        int len=subFile.length;
        bookdate=new int[len+1];
        name=new String[len+1];
        bookdate[0]=R.drawable.add;
        name[0]="新建";
        for(int i=1;i<=len;i++){
            String title=subFile[i-1].getName();
            String str[]=title.split("-");
            String bookName=str[0];
            String bg=str[1];
            bookdate[i]=Integer.valueOf(bg);
            name[i]=bookName;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==AddBook){
            if(resultCode==RESULT_CANCELED){

            }else if(resultCode==RESULT_OK){
                init();
            }
        }
    }

    class ShelfAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return bookdate.length;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView= LayoutInflater.from(getApplicationContext()).inflate(R.layout.book_item,null);
            TextView view=(TextView) convertView.findViewById(R.id.book_item_title);
            if(bookdate.length>position){
                if(position<name.length){
                    view.setText(name[position]);
                }
                view.setBackgroundResource(bookdate[position]);
            }else {
                view.setBackgroundResource(bookdate[0]);
                view.setClickable(false);
                view.setVisibility(View.INVISIBLE);
            }
            return convertView;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }
    }
}

